﻿using DataScripter.Database;
using DataScripter.Scripting;
using DataScripter.Scripting.Fluent;
using DataScripter.Scripting.InputSources;
using DataScripter.Scripting.ScriptDefinitions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

namespace DataScripter.UnitTests.EngineTests.IncludeDependencies
{
    [TestClass]
    public class WhenRequestingATableHavingThreeDependenciesAndIncludingNoDependencies
    {
        private readonly IScriptDefinition _scriptDefinition;
        private readonly IFluentTableScriptRequest _fluentTableRequest;

        public WhenRequestingATableHavingThreeDependenciesAndIncludingNoDependencies()
        {
            const string myTableName = "MyTableWithThreeDependencies";
            Mock<IDatabaseStructure> structureMock = Factories.DbStructureFactory.Create(myTableName,
                                                                                            new[]{"D1","D2","D3"}
                                                                                        );
            Engine engine = new Engine();

            IInputSource inputSource = new SqlServerInputSource("", engine);
            inputSource.RegisterType(structureMock.Object);
            engine.RegisterType(inputSource);


            _scriptDefinition = new ScriptDefinition("", engine);
            engine.RegisterType(_scriptDefinition);

            _fluentTableRequest = _scriptDefinition.RequestScript(myTableName).IncludeDependencies(new IForeignKey[0]);
        }

        [TestMethod]
        public void TableShouldBeAppendedToTablesCollection()
        {
            Assert.AreEqual(1, _scriptDefinition.ScriptRequests.Count);
        }

        [TestMethod]
        public void TableScriptRequestShouldExistInCollection()
        {
            Assert.IsTrue(_scriptDefinition.ScriptRequests.ContainsKey(_fluentTableRequest.Request.TableName));
        }

        [TestMethod]
        public void TableScriptRequestShouldBeTheSameInCollection()
        {
            Assert.IsTrue(_scriptDefinition.ScriptRequests[_fluentTableRequest.Request.TableName] == _fluentTableRequest.Request);
        }

    }
}
